package com.xinyuan.digit.next.subscriber.db.pojo;

/**
 * SQL条件类型
 *
 * @author LiuChenghui 2025/10/23 14:55:00
 * @version 1.0
 */
public enum Operator {
    // 等于
    EQ("=") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " = #{conditions[" + paramIndex + "].value}";
        }
    },

    // 不等于
    NE("!=") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " != #{conditions[" + paramIndex + "].value}";
        }
    },

    // 大于
    GT(">") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " > #{conditions[" + paramIndex + "].value}";
        }
    },

    // 小于
    LT("<") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " < #{conditions[" + paramIndex + "].value}";
        }
    },

    // 大于等于
    GE(">=") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " >= #{conditions[" + paramIndex + "].value}";
        }
    },

    // 小于等于
    LE("<=") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " <= #{conditions[" + paramIndex + "].value}";
        }
    },

    // 模糊查询
    LIKE("LIKE") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " LIKE CONCAT('%', #{conditions[" + paramIndex + "].value}, '%')";
        }
    },

    // 左模糊查询
    LIKE_LEFT("LIKE") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " LIKE CONCAT('%', #{conditions[" + paramIndex + "].value})";
        }
    },

    // 右模糊查询
    LIKE_RIGHT("LIKE") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " LIKE CONCAT(#{conditions[" + paramIndex + "].value}, '%')";
        }
    },

    // 范围查询
    BETWEEN("BETWEEN") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " BETWEEN #{conditions[" + paramIndex + "].value[0]} AND #{conditions[" + paramIndex + "].value[1]}";
        }
    },

    // IN 查询
    IN("IN") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " IN " + "<foreach collection='conditions[" + paramIndex + "].value' item='item' open='(' separator=',' close=')'>" + "#{item}" + "</foreach>";
        }
    },

    // IS NULL
    IS_NULL("IS NULL") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " IS NULL";
        }
    },

    // IS NOT NULL
    IS_NOT_NULL("IS NOT NULL") {
        @Override
        public String toSQL(String columnName, int paramIndex) {
            return columnName + " IS NOT NULL";
        }
    };

    private final String symbol;

    Operator(String symbol) {
        this.symbol = symbol;
    }

    /**
     * 获取操作符符号
     */
    public String getSymbol() {
        return symbol;
    }

    /**
     * 生成SQL片段
     *
     * @param columnName 列名
     * @param paramIndex 参数索引
     * @return SQL片段
     */
    public abstract String toSQL(String columnName, int paramIndex);

    /**
     * 根据符号获取操作符
     */
    public static Operator fromSymbol(String symbol) {
        for (Operator operator : values()) {
            if (operator.symbol.equals(symbol)) {
                return operator;
            }
        }
        throw new IllegalArgumentException("未知的操作符: " + symbol);
    }

    /**
     * 判断是否需要参数值
     */
    public boolean requiresValue() {
        return this != IS_NULL && this != IS_NOT_NULL;
    }

    /**
     * 判断是否为 BETWEEN 操作符
     */
    public boolean isBetween() {
        return this == BETWEEN;
    }

    /**
     * 判断是否为 IN 操作符
     */
    public boolean isIn() {
        return this == IN;
    }

    /**
     * 判断是否为 LIKE 系列操作符
     */
    public boolean isLike() {
        return this == LIKE || this == LIKE_LEFT || this == LIKE_RIGHT;
    }
}
